package com.itgm.jiayun_web_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.pojo.Room;
import com.itgm.jiayun_web_project.pojo.Tags;
import com.itgm.jiayun_web_project.service.RoomService;
import com.itgm.jiayun_web_project.service.TagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tags")
@Slf4j
@Tag(name = "标签管理", description = "标签管理")
public class TagsController {

    @Autowired
    private TagsService tagsService;

    //分页查询教室列表
    @GetMapping("/page")
    @Operation(summary = "分页查询标签列表")
    public Result<PageResult<Tags>> page(
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name
    ) {
        log.info("TagsController.page");
        //1.获取分页参数
        Page<Tags> TagsPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Tags> rqw = new LambdaQueryWrapper<>();
        if (name != null && !name.equals("")) {
            rqw.like(Tags::getName, name);
        }
        //2.分页查询
        Page<Tags> page1 = tagsService.page(TagsPage, rqw);
        PageResult<Tags> roomPageResult = new PageResult<>();
        roomPageResult.setTotal(page1.getTotal());
        roomPageResult.setRecords(page1.getRecords());
        return Result.success(roomPageResult);
    }

   //添加标签
    @PostMapping
    @Operation(summary = "添加标签")
    public Result<Tags> add(@RequestBody Tags tags) {
        log.info("TagsController.add");
        tagsService.save(tags);
        return Result.success(tags);
    }

}
